package com.jindy.module.AssetRepair.service.impl;

import java.util.Date;
import java.util.List;

import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.utils.DateUtils;
import com.jindy.module.AssetCheck.domain.AssetCheck;
import com.jindy.module.AssetCheck.mapper.AssetCheckMapper;
import com.jindy.module.AssetDeclared.domain.AssetDeclared;
import com.jindy.module.AssetDeclared.mapper.AssetDeclaredMapper;
import com.jindy.module.AssetDetails.domain.AssetDetails;
import com.jindy.module.AssetDetails.mapper.AssetDetailsMapper;
import com.jindy.module.AssetReceive.domain.AssetReceive;
import com.jindy.module.AssetReceive.mapper.AssetReceiveMapper;
import com.jindy.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jindy.module.AssetRepair.mapper.AssetRepairMapper;
import com.jindy.module.AssetRepair.domain.AssetRepair;
import com.jindy.module.AssetRepair.service.IAssetRepairService;

/**
 * 资产报修Service业务层处理
 * 
 * @author jindy
 * @date 2023-04-24
 */
@Service
public class AssetRepairServiceImpl implements IAssetRepairService 
{
    @Autowired
    private AssetRepairMapper assetRepairMapper;

    @Autowired
    private AssetDetailsMapper assetDetailsMapper;

    @Autowired
    private AssetReceiveMapper assetReceiveMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private AssetCheckMapper assetCheckMapper;

    @Autowired
    private AssetDeclaredMapper assetDeclaredMapper;

    /**
     * 查询资产报修
     * 
     * @param id 资产报修主键
     * @return 资产报修
     */
    @Override
    public AssetRepair selectAssetRepairById(Long id)
    {
        return assetRepairMapper.selectAssetRepairById(id);
    }

    /**
     * 查询资产报修列表
     * 
     * @param assetRepair 资产报修
     * @return 资产报修
     */
    @Override
    public List<AssetRepair> selectAssetRepairList(AssetRepair assetRepair)
    {
        return assetRepairMapper.selectAssetRepairList(assetRepair);
    }

    /**
     * 新增资产报修
     * 
     * @param assetRepair 资产报修
     * @return 结果
     */
    @Override
    public int insertAssetRepair(AssetRepair assetRepair)
    {
        int flag=0;
        AssetReceive assetReceive=null;

        //资产领用直接报修入口
        if(assetRepair.getAssetReceiveId() != null) {
            //获取资产领用信息
            assetReceive = assetReceiveMapper.selectAssetReceiveById(assetRepair.getAssetReceiveId());
            assetRepair.setAssetId(assetReceive.getReceiveAssetId());
            assetRepair.setAssetName(assetReceive.getReceiveAssetName());
            assetRepair.setClassName(assetReceive.getReceiveClassName());
            assetRepair.setAssetBrand(assetReceive.getReceiveAssetBrand());
            assetRepair.setAssetModel(assetReceive.getReceiveAssetModel());
            assetRepair.setUserId(assetReceive.getReceiveUserId());
            assetRepair.setUserName(assetReceive.getReceiveUserName());
            assetRepair.setUserDeptId(assetReceive.getReceiveDeptId());
            assetRepair.setUserDeptName(assetReceive.getReceiveDeptName());
            assetRepair.setAssetQuantity(assetReceive.getReceiveQuantity());

            //资产领用状态修改
            if(!assetReceive.getIsReturn().equals("维修中")){
                assetReceive.setIsReturn("维修中");
                assetReceiveMapper.updateAssetReceive(assetReceive);
            }
        }

        //资产明细报修入口
        else{
            if(assetRepair.getAssetId() != null){
                AssetDetails assetDetails=assetDetailsMapper.selectAssetDetailsById(assetRepair.getAssetId());
                //数量校验
                if(assetRepair.getAssetQuantity() <= assetDetails.getAssetAvailQuantity()) {
                    assetRepair.setAssetName(assetDetails.getAssetName());
                    assetRepair.setClassName(assetDetails.getClassName());
                    assetRepair.setAssetBrand(assetDetails.getAssetBrand());
                    assetRepair.setAssetModel(assetDetails.getAssetModel());

                    //修改资产明细表可用数量
                    assetDetails.setAssetAvailQuantity(assetDetails.getAssetAvailQuantity()-assetRepair.getAssetQuantity());
                    assetDetailsMapper.updateAssetDetails(assetDetails);
                }
            }
        }

        if(assetRepair.getRepairPerId() != null){
            SysUser sysUser=sysUserMapper.selectUserById(assetRepair.getRepairPerId());
            assetRepair.setRepairPerName(sysUser.getUserName());
            assetRepair.setRepairPerDeptId(sysUser.getDeptId());
            assetRepair.setRepairPerDeptName(sysUser.getDept().getDeptName());
        }

        if(assetRepair.getRespPerId() != null){
            SysUser sysUser=sysUserMapper.selectUserById(assetRepair.getRespPerId());
            assetRepair.setRespPerName(sysUser.getUserName());
            assetRepair.setRespPerDeptId(sysUser.getDeptId());
            assetRepair.setRespPerDeptName(sysUser.getDept().getDeptName());
        }



        assetRepair.setCreateTime(DateUtils.getNowDate());
        assetRepair.setRepairStatus("维修中");

        assetRepair.setCreateTime(DateUtils.getNowDate());
        flag=assetRepairMapper.insertAssetRepair(assetRepair);

        //资产盘点表新增资产报修
        AssetCheck assetCheck=new AssetCheck();
        assetCheck.setAssetId(assetRepair.getAssetId());
        assetCheck.setAssetName(assetRepair.getAssetName());
        assetCheck.setAssetName(assetRepair.getAssetName());
        assetCheck.setOperateId(assetRepair.getId());
        assetCheck.setOperateType("资产报修");
        assetCheck.setOperateTime(DateUtils.getNowDate());
        assetCheckMapper.insertAssetCheck(assetCheck);

        return flag;
    }

    /**
     * 修改资产报修
     * 
     * @param assetRepair 资产报修
     * @return 结果
     */
    @Override
    public int updateAssetRepair(AssetRepair assetRepair)
    {
        assetRepair.setUpdateTime(DateUtils.getNowDate());
        return assetRepairMapper.updateAssetRepair(assetRepair);
    }

    /**
     * 批量删除资产报修
     * 
     * @param ids 需要删除的资产报修主键
     * @return 结果
     */
    @Override
    public int deleteAssetRepairByIds(Long[] ids)
    {
        return assetRepairMapper.deleteAssetRepairByIds(ids);
    }

    /**
     * 删除资产报修信息
     * 
     * @param id 资产报修主键
     * @return 结果
     */
    @Override
    public int deleteAssetRepairById(Long id)
    {
        return assetRepairMapper.deleteAssetRepairById(id);
    }

    /**
     * 查询资产报修
     *
     * @param  assetReceiveId 资产领用id
     * @return 资产报修
     */
    @Override
    public AssetRepair selectAssetRepairByAssetReceiveId(Long assetReceiveId){
        return assetRepairMapper.selectAssetRepairByAssetReceiveId(assetReceiveId);
    }

    /**
     * 维修成功
     *
     * @param  assetRepair 资产维修对象
     * @return 资产报修
     */
    @Override
    public int updateRepairSuccess(AssetRepair assetRepair){
        int flag=0;
        if(assetRepair.getId() != null && assetRepair.getRepairStatus().equals("维修中")){
            //修改维修表状态
            assetRepair.setRepairStatus("维修成功！");
            assetRepair.setUpdateTime(DateUtils.getNowDate());
            flag=assetRepairMapper.updateAssetRepair(assetRepair);

            if (assetRepair.getAssetReceiveId() != null && flag == 1){
                //修改领用表状态
                AssetReceive assetReceive=assetReceiveMapper.selectAssetReceiveById(assetRepair.getAssetReceiveId());
                assetReceive.setIsReturn("使用中");
                assetReceive.setUpdateTime(DateUtils.getNowDate());
                flag=assetReceiveMapper.updateAssetReceive(assetReceive);
            }

            if (assetRepair.getAssetReceiveId() == null && flag == 1){
                //修改资产明细表可用数量
                AssetDetails assetDetails=assetDetailsMapper.selectAssetDetailsById(assetRepair.getAssetId());
                assetDetails.setAssetAvailQuantity(assetDetails.getAssetAvailQuantity()+assetRepair.getAssetQuantity());
                assetDetails.setUpdateTime(DateUtils.getNowDate());
                flag=assetDetailsMapper.updateAssetDetails(assetDetails);
            }
        }

        return flag;
    }

    /**
     * 维修失败，直接报废
     *
     * @param  assetRepair 资产维修对象
     * @return 资产报修
     */
    @Override
    public int updateRepairFail(AssetRepair assetRepair){
        int flag=0;
        if(assetRepair.getId() != null && assetRepair.getRepairStatus().equals("维修中")){
            assetRepair.setRepairStatus("已报废");
            assetRepair.setUpdateTime(DateUtils.getNowDate());
            flag=assetRepairMapper.updateAssetRepair(assetRepair);

            //资产盘点表新增资产
            AssetCheck assetCheck=new AssetCheck();
            assetCheck.setAssetId(assetRepair.getAssetId());
            assetCheck.setAssetName(assetRepair.getAssetName());
            assetCheck.setOperateId(assetRepair.getId());
            assetCheck.setOperateType("资产报废");
            assetCheck.setOperateTime(DateUtils.getNowDate());
            assetCheckMapper.insertAssetCheck(assetCheck);

            //资产报废新增
            AssetDeclared assetDeclared=new AssetDeclared();
            assetDeclared.setDeclaredAssetId(assetRepair.getAssetId());
            assetDeclared.setDeclaredAssetName(assetRepair.getAssetName());
            assetDeclared.setDeclaredAssetClassName(assetRepair.getClassName());
            assetDeclared.setAssetBrand(assetRepair.getAssetBrand());
            assetDeclared.setAssetModel(assetRepair.getAssetModel());
            assetDeclared.setDeclaredQuantity(assetRepair.getAssetQuantity());
            assetDeclared.setDeclaredReason("维修失败");
            assetDeclared.setCreateTime(DateUtils.getNowDate());
            if(assetRepair.getAssetReceiveId() != null){
                assetDeclared.setDeclaredReceiveId(assetRepair.getAssetReceiveId());
                assetDeclared.setDeclaredUserId(assetRepair.getUserId());
                assetDeclared.setDeclaredUserName(assetRepair.getUserName());
                assetDeclared.setDeclaredUserDeptId(assetRepair.getUserDeptId());
                assetDeclared.setDeclaredUserDeptName(assetRepair.getUserDeptName());
            }
            assetDeclaredMapper.insertAssetDeclared(assetDeclared);


            //修改资产领用表状态
            if(assetRepair.getAssetReceiveId() != null && flag == 1){
                AssetReceive assetReceive=assetReceiveMapper.selectAssetReceiveById(assetRepair.getAssetReceiveId());
                assetReceive.setIsReturn("已报废");
                flag=assetReceiveMapper.updateAssetReceive(assetReceive);
            }

            //修改资产明细表状态
            if(assetRepair.getAssetReceiveId() == null && flag == 1){
                AssetDetails assetDetails=assetDetailsMapper.selectAssetDetailsById(assetRepair.getAssetId());
                assetDetails.setAssetAvailQuantity(assetDetails.getAssetAvailQuantity()+assetRepair.getAssetQuantity());
                flag=assetDetailsMapper.updateAssetDetails(assetDetails);
            }
        }
        return flag;
    }

}
